﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using COFE.Core.Utils;
using COFE.Core.Filter;

namespace COFE.Core.EntryType
{
    public class EntryTypeDictionary : IEntryTypeDictionary
    {
        #region Constructor

        public EntryTypeDictionary(IEntryTypeIconExtractor iconExtractor, IEntryFilterGenerator filterGenerator)
        {
            _iconExtractor = iconExtractor;
            _filterGenerator = filterGenerator;
        }


        #endregion

        #region Methods       

        private IEntryTypeInfo getEti(IFileSystemInfo entry)
        {            
            var filters = FilterGenerator.GenerateFilters(entry);
            var id = filters.GetId();
            lock (_etiDic)
            {
                if (_etiDic.ContainsKey(id))
                    return _etiDic[id];
            }

            var newEti = this.CreateEti(entry);

            lock (_etiDic)
            {
                if (!_etiDic.ContainsKey(id))
                    _etiDic.Add(id, newEti);

                return _etiDic[id];
            }
        }

        private IEntryTypeInfo getEti(Guid key)
        {
            lock (_etiDic)
            {
                if (_etiDic.ContainsKey(key))
                    return _etiDic[key];
                else return null;
            }
        }

        #endregion

        #region Data

        private IEntryTypeIconExtractor _iconExtractor;
        private IEntryFilterGenerator _filterGenerator;
        private Dictionary<Guid, IEntryTypeInfo> _etiDic = new Dictionary<Guid, IEntryTypeInfo>();

        #endregion

        #region Public Properties
        public IEntryTypeInfo this[Guid key] { get { return getEti(key); } }
        public IEntryTypeInfo this[IFileSystemInfo entry] { get { return getEti(entry); } }
        public IEntryTypeIconExtractor IconExtractor { get { return _iconExtractor; } private set { _iconExtractor = value; } }
        public IEntryFilterGenerator FilterGenerator { get { return _filterGenerator; } private set { _filterGenerator = value; } }

        #endregion





    }
}
